
package hk.ces.oxfam.patch.result;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * @author ReutyCheng
 */
abstract class AbstractSQLResult
{
  protected final Set<String> tableNames;
  protected final String[] columnNames;
  protected final List<Object[]> resultsList;

  public AbstractSQLResult(ResultSet resultSet) throws SQLException
  {
    ResultSetMetaData metaData = resultSet.getMetaData();
    int columnCount = metaData.getColumnCount();

    this.tableNames = new HashSet<String>();
    this.columnNames = new String[columnCount];
    for (int index = 0; index < columnCount; index++)
    {
      int columnIndex = index + 1;
      this.columnNames[index] = metaData.getColumnLabel(columnIndex);
      this.tableNames.add(metaData.getTableName(columnIndex));
    }

    this.resultsList = new ArrayList<Object[]>();
    while (resultSet.next())
    {
      Object[] results = new Object[columnCount];
      for (int index = 0; index < columnCount; index++)
      {
        results[index] = resultSet.getObject(index + 1);
      }
      this.resultsList.add(results);
    }
  }
}
